package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 搜索文章
     */
    @RequestMapping(value ="/search/{keywords}/{page}/{size}" ,method = RequestMethod.GET)
    public Result findSearch(@PathVariable String keywords,@PathVariable int page,@PathVariable int size){

        Page<Article> list = articleService.findSearch(keywords,page,size);

        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(list.getTotalElements(),list.getContent()));
    }
}
